#include "stdafx.h"
#include <fstream>

TEST(str, find)
{
	string s = " a b  d   e ";
	EXPECT_EQ(0, s.find(" ", 0));
	EXPECT_EQ(2, s.find(" ", 1));
	EXPECT_EQ(4, s.find(" ", 3));
	EXPECT_EQ(5, s.find(" ", 5));
	EXPECT_EQ(7, s.find(" ", 6));
	EXPECT_EQ(8, s.find(" ", 8));
	EXPECT_EQ(9, s.find(" ", 9));
	EXPECT_EQ(11, s.find(" ", 10));

	string s2 = "a";
	EXPECT_EQ(string::npos, s2.find(" ", 1));
	EXPECT_EQ("a", s2.substr(0, 1));
}

TEST(str, sub)
{
	string s = " a b";
	EXPECT_EQ(" ", s.substr(0, 1));
	EXPECT_EQ("a", s.substr(1, 1));
	EXPECT_EQ("a ", s.substr(1, 2));
}




TEST(str, word)
{
	string s = " a b  c";
	string::iterator iter = s.begin();
	string word = "";
	while (iter != s.end())
	{
		char cv = *iter;
		++iter;
		if (' ' == cv || '\t' == cv || '\n' == cv)  
		{
			//word = s
			continue;
		}
	}
}
TEST(str, getline)
{
	string s = "b c d";
	stringstream ss(s);
	string item;
	while(getline(ss, item, ' '))
	{
		cout<<"["<<item<<"]";
	}
	
}

//TEST(str, tok)
//{
//  char str[] ="- This, a sample string.";
//  char * pch;
//  printf ("Splitting string \"%s\" into tokens:\n",str);
//  pch = strtok(str," ,.-");
//  while (pch != NULL)
//  {
//    printf ("%s\n",pch);
//    pch = strtok(NULL, " ,.-");
//  }
//}
TEST(str, cha)
{
	char A = 'A';
	char Z = 'Z';
	char a = 'a';
	char z = 'z';
	int i = A;
	EXPECT_EQ(65, A);
	i = Z;
	EXPECT_EQ(90, Z);
	i = a;
	EXPECT_EQ(97, a);
	i = z;
	EXPECT_EQ(122, z);

	char one = '1';
	char zero = '0';
	char nine = '9';
	i = one;
	EXPECT_EQ(49, i);
	i = zero;
	EXPECT_EQ(48, i);
	i = nine;
	EXPECT_EQ(57, i);
}

void split(string& str, string& spa, size_t& last_pos)
{
	const size_t& pos = str.find_first_of(spa, last_pos);
	string sub =  str.substr(last_pos, pos - last_pos);
	if (!sub.empty())
	{
		if (string::npos == sub.find(" ", 0))
		{
			cout<<"["<<sub<<"]"<<endl;
		} else
		{
			string espa = " ";
			size_t elp = 0;
			split(sub, espa, elp);
		}
	}
	if (string::npos == pos)
	{
		return;
	}
	last_pos = pos + 1;
	split(str, spa, last_pos);
}
TEST(str, split)
{
	string  str = " a  b  c   d    efg  123	45w\tswdw54  ";
	//string str = "	a		 b  k e  		c\td\t\tf\t";
	string spa = "\t";
	size_t last_pos = 0;
	split(str, spa, last_pos);
}